//
//  GoodsViewController.m
//  lisure0.1
//
//  Created by xalo on 16/4/25.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "GoodsViewController.h"
#import "GoodsModel.h"
#import "TopicModel.h"
#import "GoodsCell.h"
#import "TopicDetailViewController.h"

#define kGoodsURL @"http://api2.pianke.me/pub/shop"

@interface GoodsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray* modelDataArr;
@property(nonatomic, assign)NSInteger *total;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation GoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self requestGoodsData];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuBtn"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(presentLeftMenuViewController:)];
    [self.tableView registerNib:[UINib nibWithNibName:@"GoodsCell" bundle:nil] forCellReuseIdentifier:@"GoodsCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)modelDataArr{

    if (!_modelDataArr) {
        _modelDataArr = [NSMutableArray array];
    }
    return _modelDataArr;
}

-(void)requestGoodsData{

    [self fetchDatawith:kGoodsURL parameter:nil finished:^(NSDictionary *dic) {
        NSNumber* total = dic[@"data"][@"total"];
        self.total = [total integerValue];
        NSArray* array = dic[@"data"][@"list"];
        for (int i = 0; i < array.count; i ++) {

            GoodsModel* model = [[GoodsModel alloc] init];
            [model setValuesForKeysWithDictionary:array[i]];
            [self.modelDataArr addObject:model];
        }
        NSLog(@"%@",self.modelDataArr);
        [self.tableView reloadData];
    }];
}

-(void)fetchDatawith:(NSString*)url parameter:(NSDictionary*)dic finished:(finished)finished{

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"良品数据请求成功");
        NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        finished(dic);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"良品数据请求失败");
        [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];
}

#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [tableView showMessage:@"暂无数据" byDataSourceCount:self.modelDataArr.count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    GoodsCell* cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsCell" forIndexPath:indexPath];
    [cell setcellWithModel:self.modelDataArr[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 300;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    TopicModel* model = [[TopicModel alloc] init];
    model.title = [[self.modelDataArr objectAtIndex:indexPath.row] title];
    model.contentid = [[self.modelDataArr objectAtIndex:indexPath.row] contentid];
    TopicDetailViewController* topic = [[TopicDetailViewController alloc] init];
    topic.model = model;
    [self.navigationController pushViewController:topic animated:YES];

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
